//
//  kWFixedCell.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 2017/5/30.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "kWFixedCell.h"


@interface kWFixedCell()

@property(nonatomic,strong)NSMutableArray <kWHomeModel *>*modelArray;

@end

@implementation kWFixedCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setData:(NSArray<kWHomeModel *> *)data
{
    _data = data;

    for (int i = 0; i < 3; ++i) {
        
        kWHomeModel *model = [[kWHomeModel alloc] initWithDictionary:(NSDictionary *)data[i] error:nil];
        [self.modelArray addObject:model];
        
        UIImageView *toolView = [[UIImageView alloc] init];
        [toolView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        [self addSubview:toolView];
        toolView.userInteractionEnabled = true;
        toolView.tag = i;
        [toolView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fixedClick:)]];
        [self.contentView addSubview:toolView];
        
        CGFloat margin = 15;
        CGFloat viewW = (WIDTH-40)/2;
        WeakSelf(self);
        if (i == 0) {
            
            [toolView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakself).offset(margin);
                make.width.mas_equalTo(viewW);
                make.height.mas_equalTo(HEIGHT/4);
                make.top.equalTo(weakself.contentView.mas_top);
            }];
        }else if (i == 1){
            
            [toolView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakself).offset(margin+viewW+10);
                make.width.mas_equalTo(viewW);
                make.height.mas_equalTo((HEIGHT/4-10)/2);
                make.top.equalTo(weakself.contentView.mas_top);
            }];
        }else{
            
            [toolView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakself).offset(margin+viewW+10);
                make.width.mas_equalTo(viewW);
                make.height.mas_equalTo((HEIGHT/4-10)/2);
                make.top.equalTo(weakself.contentView.mas_top).offset((HEIGHT/4-10)/2+10);
            }];
        }
        
    }

}


- (void)fixedClick:(UIGestureRecognizer *)recongnizer
{
    if (self.fixedBlock) {
        self.fixedBlock(self.modelArray[recongnizer.view.tag]);
    }
}

- (NSMutableArray <kWHomeModel *>*)modelArray
{
    if (!_modelArray) {
        _modelArray = [NSMutableArray arrayWithCapacity:3];
    }
    return _modelArray;
    
}

@end
